package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.HotSearchRequest;
import com.ytjj.qmyx.admin.service.HotSearchService;
import com.ytjj.qmyx.mall.model.HotSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 热搜配置
 */
@RestController
@RequestMapping("/hotSearch")
public class HotSearchController {

    @Autowired
    private HotSearchService hotSearchService;

    /**
     * 热搜词搜索
     */
    @PostMapping("/list")
    @OperLog(operModule = "热搜管理", operDesc = "热搜列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult list() {
        List<HotSearch> list = hotSearchService.list();
        return CommonResult.success(list);
    }

    /**
     * 新增热搜词
     */
    @PostMapping("/addHotSearch")
    @OperLog(operModule = "热搜管理", operDesc = "新增热搜词", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult add(@RequestBody HotSearchRequest hotSearchRequest) {
        int count = hotSearchService.add(hotSearchRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改热搜词
     */
    @PostMapping("/updateHotSearch")
    @OperLog(operModule = "热搜管理", operDesc = "编辑热搜", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateHotSearch(@RequestBody HotSearchRequest hotSearchRequest) {
        int count = hotSearchService.updateHotSearch(hotSearchRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     * 删除热搜词
     */
    @PostMapping("/deleteById")
    @OperLog(operModule = "热搜管理", operDesc = "删除热搜词", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteById(@RequestBody HotSearchRequest hotSearchRequest){
        int count = hotSearchService.deleteById(hotSearchRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
